var express = require('express');
var router = express.Router();

//自定义商品数据
var products = [
    {
        id: 1,
        image: '/images/11411.png',
        title:'商品1',
        subTitle: '描述1'
    },
    {
        id: 2,
        image: '/images/500210.png',
        title: '商品2',
        subTitle: '描述2'
    },
    {
        id: 3,
        image: '/images/500211.png',
        title:'商品3',
        subTitle: '描述3'
    },
    {
        id: 4,
        image: '/images/515525.png',
        title:'商品4',
        subTitle: '描述4'
    },
    {
        id: 5,
        image: '/images/567478.png',
        title:'商品5',
        subTitle: '描述5'
    },
    {
        id: 6,
        image: '/images/1090439.png',
        title:'商品6',
        subTitle: '描述6'
    },
    {
        id: 7,
        image: '/images/1145719.png',
        title:'商品7',
        subTitle: '描述7'
    }
]
//获取商品列表
router.get('/',function(req,res,next){
    res.send(JSON.stringify(products));
});
//新建商品接口
router.post('/',function(req,res,next){
    products = products.concat(req.body);
    res.send(JSON.stringify(products));
});
//更新商品接口
router.put('/:id',function(req,res,next){
    for(var i=0;i<products.length;i++){
        if(products[i].id === parseInt(req.params.id)){
            products[i] = req.body;
        }
    }
    res.send(JSON.stringify(products));
});
router.delete('/:id',function(req,res,next){
    for(var i=0;i<products.length;i++){
        if(products[i].id === parseInt(req.params.id)){
            products.splice(i,1);
        }
    }
    res.send(JSON.stringify(products));
});
module.exports = router